﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using AppLayer;
using SpotItComponents;
using SpotItServerAPI;
using Vitruvian.Services;

namespace SpotIt
{
    /// <summary>
    /// Interaction logic for SpoIt_Single_Menu.xaml
    /// </summary>
    public partial class SpoIt_Single_Menu : Window
    {
        Session session;
        Services services;
        List<Deck> decks;

        public bool Play { get; set; }
        AudioFiles theAudioFiles = AudioFiles.Instance;
        public SpoIt_Single_Menu(Session session, Services services)
        {
            InitializeComponent();

            this.session = session;
            this.services = services;
            decks = new List<Deck>();
        }

        private void timeTrialRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if ((bool)timeTrialRadioButton.IsChecked)
            {
                timeTrialCanvas.Visibility = Visibility.Visible;
                survivalCanvas.Visibility = Visibility.Hidden;
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            timeTrialCanvas.Visibility = Visibility.Hidden;
            survivalCanvas.Visibility = Visibility.Hidden;

            OperationResult result = services.deckManager.Lookup(session, null, null, null);
            if (result.Success)
                decks = result.Data as List<Deck>;

            deckDataGrid.CanUserAddRows = false;
            deckDataGrid.CanUserDeleteRows = false;
            deckDataGrid.CanUserResizeRows = false;
            deckDataGrid.SelectionMode = DataGridSelectionMode.Single;
            deckDataGrid.ItemsSource = decks;
            difficultyComboBox.Text = "Easy";
            diffucultyComboBox.Text = "Easy";
            deckCardCountLabel.Content = "Number of Cards: " + (int)cardSlider.Value * 5 + 5;
            timeLabel.Content = "5" + " Seconds";
        }

        private void survivalRadioButton_Checked(object sender, RoutedEventArgs e)
        {
            if ((bool)survivalRadioButton.IsChecked)
            {
                timeTrialCanvas.Visibility = Visibility.Hidden;
                survivalCanvas.Visibility = Visibility.Visible;
            }
        }

        private void playButton_Click(object sender, RoutedEventArgs e)
        {
            // TODO: Gather settings information
            theAudioFiles.theFiles[0].Stop();
            // TODO: pass the info onto the play form
            Play = true;
            this.Close();
        }

        private void cardSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int number = (int)cardSlider.Value;
            int cards = number * 5 + 5;
            numberOfCardsLabel.Content = cards.ToString() +  " Cards";
        }

        private void timeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            int number = (int)timeSlider.Value;
            int seconds = number * 10 + number * 2;
            if (seconds == 0)
                seconds = 5;
            timeLabel.Content = seconds.ToString() + " Seconds";
        }

        private void highScoreExpander_Expanded(object sender, RoutedEventArgs e)
        {
            deckGroupBox.Margin = new Thickness(239, 124, 0, 0);

        }

        private void highScoreExpander_Collapsed(object sender, RoutedEventArgs e)
        {
            deckGroupBox.Margin = new Thickness(110, 124, 0, 0);
        }

        private void deckDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Deck selectedDeck = (Deck)deckDataGrid.SelectedItem;
            deckTitleLabel.Content = "Name: " + selectedDeck.Label;
            List<Card> cards = selectedDeck.Cards;
            deckCardCountLabel.Content = "Number Of Cards: " + cards.Count;

        }
    }
}
